package flc.ast.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.os.Bundle;
import android.view.View;

import com.blankj.utilcode.util.ImageUtils;
import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.reflect.TypeToken;
import com.youth.banner.listener.OnBannerListener;
import com.youth.banner.listener.OnPageChangeListener;

import java.util.ArrayList;
import java.util.List;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.AlbumBannerAdapter;
import flc.ast.adapter.AlbumDetailAdapter;
import flc.ast.bean.MyAlbumBean;
import flc.ast.bean.MyAlbumItemBean;
import flc.ast.databinding.ActivityAlbumDetailBinding;
import flc.ast.utils.AppSpUtils;
import io.reactivex.rxjava3.core.ObservableEmitter;
import stark.common.basic.utils.RxUtil;
import stark.common.basic.utils.SPUtil;

public class AlbumDetailActivity extends BaseAc<ActivityAlbumDetailBinding> {

    public static MyAlbumBean myBean = null;
    private AlbumDetailAdapter detailAdapter;
    private int oldPosition = 0, selPosition = 0;
    private AlbumBannerAdapter bannerAdapter;

    @Override
    protected int onCreate() {
        return R.layout.activity_album_detail;
    }

    @Override
    protected void initView() {
        mDataBinding.tvAlbumDetailBack.setOnClickListener(v -> onBackPressed());
        mDataBinding.rvAlbumDetailList.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
        detailAdapter = new AlbumDetailAdapter();
        mDataBinding.rvAlbumDetailList.setAdapter(detailAdapter);
        detailAdapter.setOnItemClickListener(this);


    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {

        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        detailAdapter.getItem(oldPosition).setSelect(false);
        detailAdapter.notifyItemChanged(oldPosition);
        oldPosition = position;
        detailAdapter.getItem(position).setSelect(true);
        detailAdapter.notifyItemChanged(position);
        mDataBinding.banner.setCurrentItem(position);
    }

    @Override
    protected void initData() {
        if (myBean == null) return;
        mDataBinding.tvAlbumDetailBack.setText(myBean.getName());
    }

    @Override
    protected void onResume() {
        super.onResume();
        getData();
    }

    //获取视频和图片数据
    private void getData() {
        showDialog(getString(R.string.get_data_ing));
        oldPosition = 0;
        RxUtil.create(new RxUtil.Callback<List<MyAlbumItemBean>>() {
            @Override
            public void doBackground(ObservableEmitter<List<MyAlbumItemBean>> emitter) {
                List<MyAlbumItemBean> list = new ArrayList<>();
                List<MyAlbumBean> listSp = AppSpUtils.getAlbumList();
                if (listSp != null && listSp.size() > 0) {
                    for (MyAlbumBean bean : listSp) {
                        if (bean.getId().equals(myBean.getId())) {
                            list.addAll(bean.getList());
                        }
                    }
                }
                list.get(0).setSelect(true);
                emitter.onNext(list);
            }

            @Override
            public void accept(List<MyAlbumItemBean> list) {
                dismissDialog();
                if (list != null && list.size() > 0) {
                    detailAdapter.setList(list);
                    mDataBinding.rvAlbumDetailList.setVisibility(View.VISIBLE);
                    setData();
                }
            }
        });
    }

    private void setData() {
        mDataBinding.banner.isAutoLoop(false);
        bannerAdapter = new AlbumBannerAdapter(detailAdapter.getValidData());
        mDataBinding.banner.setAdapter(bannerAdapter, false);//false不无限循环
        mDataBinding.banner.addOnPageChangeListener(new OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                detailAdapter.getItem(oldPosition).setSelect(false);
                detailAdapter.notifyItemChanged(oldPosition);
                oldPosition = position;
                detailAdapter.getItem(position).setSelect(true);
                detailAdapter.notifyItemChanged(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
        mDataBinding.banner.setOnBannerListener(new OnBannerListener() {
            @Override
            public void OnBannerClick(Object data, int position) {
                if (ImageUtils.isImage(detailAdapter.getItem(position).getPath())) {
                    ImgDetailActivity.selPosition = position;
                    startActivity(ImgDetailActivity.class);
                } else {
                    VideoDetailActivity.selPosition = position;
                    startActivity(VideoDetailActivity.class);
                }
            }
        });
    }

}